use serde::{Deserialize, Serialize};
use wither::{
    bson::{doc, oid::ObjectId, DateTime},
    Model,
};

#[derive(Debug, Clone, Serialize, Deserialize, Model)]
#[model(index(
    keys = r#"doc!{"expired_at":1}"#,
    options = r#"doc!{"expireAfterSeconds":1}"#
))]
pub struct EmailCodeModel {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub email: String,
    pub code: String,
    pub created_at: DateTime,
    pub expired_at: DateTime,
}

impl Default for EmailCodeModel {
    fn default() -> Self {
        let now = chrono::Utc::now();
        Self {
            id: None,
            email: String::new(),
            code: String::new(),
            created_at: DateTime::from_chrono(now),
            expired_at: DateTime::from_chrono(now + chrono::Duration::seconds(360)),
        }
    }
}
